package com.lanou.tipsy.fragment.singer.classify;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.lanou.tipsy.R;
import com.lanou.tipsy.activity.MainActivity;
import com.lanou.tipsy.base.BaseFragment;

import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by FengJun on 15/10/20.
 * 歌手
 */
public class SingerFragment extends BaseFragment implements RadioGroup.OnCheckedChangeListener {
    public static final String TAG = "SingerFragment";
    //定义viewPager对象
    private Context mContext;
    private RadioGroup rg_sin_tab;
    private RadioButton rb_sin_top100, rb_sin_popular, rb_sin_chinese,
            rb_sin_eu, rb_sin_ja;

    private FragmentManager manager;
    private FragmentTransaction transaction;
    private Map<String, SoftReference<ArrayList<Fragment>>> softReferenceMap = new HashMap<>();
    private ArrayList<Fragment> fragments;
    private int choosed = 0;
    private int current = 0;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.mContext = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fr_singer, null);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (savedInstanceState != null) {
            current = savedInstanceState.getInt("state");
        }
        initView(getView());
    }

    //初始化->添加监听
    private void initView(View view) {
        //初始化viewpager
        rg_sin_tab = (RadioGroup) view.findViewById(R.id.rg_sin_tab);
        rb_sin_top100 = (RadioButton) view.findViewById(R.id.rb_sin_top100);
        rb_sin_popular = (RadioButton) view.findViewById(R.id.rb_sin_popular);
        rb_sin_chinese = (RadioButton) view.findViewById(R.id.rb_sin_chinese);
        rb_sin_eu = (RadioButton) view.findViewById(R.id.rb_sin_eu);
        rb_sin_ja = (RadioButton) view.findViewById(R.id.rb_sin_ja);
        //加载fragment集合
        manager = getActivity().getSupportFragmentManager();
        addFragments(TAG);
        getFragmentsByPath(TAG);
        //设置监听
        rg_sin_tab.setOnCheckedChangeListener(this);
    }

    private void addFragments(String path) {
        Log.i("test", TAG + "->添加软引用对象");
        //强引用的对象
        ArrayList<Fragment> datas = new ArrayList<>();
        datas.add(new TopFragment());
        datas.add(new PopularFragment());
        datas.add(new ChineseFragment());
        datas.add(new EuropeAmericaFragment());
        datas.add(new JapanKoreaFragment());
        //软引用fragment集合对象
        SoftReference<ArrayList<Fragment>> softReference = new SoftReference<>(datas);
        //添加该对象到map中使其缓存
        softReferenceMap.put(path, softReference);
    }

    private ArrayList<Fragment> getFragmentsByPath(String path) {
        Log.i("test", TAG + "->获取软引用对象");
        //从缓存中获取软引用的集合对象
        SoftReference<ArrayList<Fragment>> softReference = softReferenceMap.get(path);
        //判断是否存在软引用
        if (softReference == null) {
            return null;
        }
        //去除集合对象,如果由于内存不足集合对象被回收,将取得空
        fragments = softReference.get();
        return fragments;
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.rb_sin_top100:
                current = 0;
                break;
            case R.id.rb_sin_popular:
                current = 1;
                break;
            case R.id.rb_sin_chinese:
                current = 2;
                break;
            case R.id.rb_sin_eu:
                current = 3;
                break;
            case R.id.rb_sin_ja:
                current = 4;
                break;
        }

        if (choosed != current) {
            choosed = current;
            transaction = manager.beginTransaction();
            transaction.replace(R.id.tabView0, fragments.get(choosed));
            transaction.commit();
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("state", current);
    }

    @Override
    public void onResume() {
        super.onResume();
        transaction = manager.beginTransaction();
        transaction.replace(R.id.tabView0, fragments.get(current));
        transaction.addToBackStack(null);
        transaction.commit();
    }
}
